package com.lb.shopguide.ui.fragment.guide.member;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.AdapterFans;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.entity.FansBean;
import com.lb.shopguide.event.guide.FansTransToMemberEvent;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by kang on 2017/9/7.
 */

public class FragmentFansList extends BaseCommonFragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;


    private AdapterFans mAdapterFans;

    private List<FansBean> mFansList;

    public static FragmentFansList newInstance() {
        return new FragmentFansList();
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_fans_list;
    }

    @Override
    public void initData() {
        mFansList = new ArrayList<>();
        sendRequestGetFansList();
        EventBus.getDefault().register(this);
    }

    @Override
    public void initView() {
        noDataView = LayoutInflater.from(mContext).inflate(R.layout.layout_empty_view, (ViewGroup) mRecyclerView.getParent(), false);
        TextView tvEmpty = noDataView.findViewById(R.id.tv_empty);
        tvEmpty.setText("暂时没有粉丝");
        errorView = LayoutInflater.from(mContext).inflate(R.layout.layout_error_view, (ViewGroup) mRecyclerView.getParent(), false);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(253, 91, 89));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapterFans = new AdapterFans(R.layout.item_fans, mFansList);
        mRecyclerView.setAdapter(mAdapterFans);
        mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).marginProvider(mAdapterFans).paintProvider(mAdapterFans).build());

    }

    @Override
    public void finishRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void sessionOutOfDate() {
        ((GuideMainActivity) getActivity()).onSessionOutOfDate();
        finishRefresh();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
        finishRefresh();
    }

    @Override
    public String getPageTag() {
        return FragmentFansList.class.getName();
    }

    private void bindData() {
        mAdapterFans.setNewData(mFansList);
    }

    private void bindEmpty() {
        mFansList.clear();
        mAdapterFans.setNewData(mFansList);
        mAdapterFans.setEmptyView(noDataView);
    }

    private void sendRequestGetFansList() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    mFansList = new ArrayList<>();
                    JsonArray jsonFans = JsonUtil.getJsonArrayFromMap(response.getReturnContent());
                    if (jsonFans.size() != 0) {
                        for (JsonElement obj : jsonFans) {
                            FansBean bean = JsonUtil.getGson().fromJson(obj, FansBean.class);
                            mFansList.add(bean);
                        }
                        bindData();
                    } else {
                        bindEmpty();
                    }
                    finishRefresh();
                }
            }
        };
        ApiMethodGuide.getFansList(new SimpleLbObserver(listener,otherListener),this, FragmentEvent.DESTROY_VIEW);
    }

    @Override
    public void onRefresh() {
        mAdapterFans.setEnableLoadMore(false);
        if (mError) {
            mAdapterFans.setEmptyView(errorView);
            mError = false;
        } else {
            if (mNoData) {
                mAdapterFans.setEmptyView(noDataView);
                mNoData = false;
            } else {
                sendRequestGetFansList();
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshMembers(FansTransToMemberEvent event) {
        sendRequestGetFansList();
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }
}
